import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useAuthStore = defineStore('auth', () => {
  // 从localStorage读取初始登录状态
  const isLoggedIn = ref(localStorage.getItem('token') ? true : false)
  
  function login(token) {
    localStorage.setItem('token', token)
    isLoggedIn.value = true
  }
  
  function logout() {
    localStorage.removeItem('token')
    isLoggedIn.value = false
  }

  return { isLoggedIn, login, logout }
})